package com.zzit.vjv.controller.documentReview_controller.backend;

import com.zzit.vjv.biz.documentReview_biz.backend.BackendListBiz;
import com.zzit.vjv.entity.documentReview_entity.*;
import com.zzit.vjv.util.Page;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("listController")
public class ListController {


    @Resource(name = "backendListBiz")
    private BackendListBiz backendListBiz;


    /**
     * 前台信息列表控制器
     *
     * @param model
     * @param @RequestParam(value = "querySoftwareName",required = false) String querySoftwareName,@RequestParam(value = "queryFlatformId",required = false) Integer queryFlatformId,@RequestParam(value = "queryCategoryLevel1",required = false) Integer queryCategoryLevel1,@RequestParam(value = "queryCategoryLevel2",required = false) Integer queryCategoryLevel2,@RequestParam(value = "queryCategoryLevel3",required = false) Integer queryCategoryLevel3
     * @return , method = RequestMethod.POST,params = {"querySoftwareName","queryFlatformId","queryCategoryLevel1","queryCategoryLevel2","queryCategoryLevel3","userCode","userPassword"}
     */
    @RequestMapping(value = "/typeNameList")
    public String typeNameList(Model model, @RequestParam(value = "querySoftwareName", required = false) String querySoftwareName, @RequestParam(value = "queryFlatformId", required = false) Integer queryFlatformId, @RequestParam(value = "queryCategoryLevel1", required = false) Integer queryCategoryLevel1, @RequestParam(value = "queryCategoryLevel2", required = false) Integer queryCategoryLevel2, @RequestParam(value = "queryCategoryLevel3", required = false) Integer queryCategoryLevel3, @RequestParam(value = "pageIndex", required = false) Integer pageIndex) {


        //所属平台
        List<DataDictionary> flatFormList = backendListBiz.byTypeNameFindPlatform("所属平台");

        //一级联动
        List<AppCategory> categoryLevel1List = backendListBiz.byIdFindCategoryName(null);


        //所属平台
        model.addAttribute("flatFormList", flatFormList);
        //一级联动
        model.addAttribute("categoryLevel1List", categoryLevel1List);


        //将前台的条件存入至map集合,用于查询
        Map<String, Object> map = new HashMap<>();

        map.put("querySoftwareName", querySoftwareName);
        map.put("queryFlatformId", queryFlatformId);
        map.put("queryCategoryLevel1", queryCategoryLevel1);
        map.put("queryCategoryLevel2", queryCategoryLevel2);
        map.put("queryCategoryLevel3", queryCategoryLevel3);

        //用于分页的总条数
        List<AppInfo_result> appInfo1 = backendListBiz.findAppInfoList(map.size() > 0 ? map : null);

        //分页帮助类
        Page page = new Page(appInfo1.size(), 2, pageIndex == null ? 1 : pageIndex);

        //用于分页查询
        map.put("CurrentPageNo", (page.getCurrentPageNo() - 1) * page.getPageSize());
        map.put("PageSize", page.getPageSize());


        //调用前台app信息列表方法
        List<AppInfo_result> appInfo = backendListBiz.findAppInfoList(map.size() > 0 ? map : null);


        System.out.println("所属平台:" + queryFlatformId + "," + querySoftwareName + "," + queryCategoryLevel1);

        //传输前台listapp信息列表
        model.addAttribute("appInfoList", appInfo);


        //传输分页类
        model.addAttribute("pages", page);

        //用于分页条件
        model.addAttribute("querySoftwareName", querySoftwareName);
        model.addAttribute("queryFlatformId", queryFlatformId);


        //三级联动刷新保持不变
        List<AppCategory> categoryLevel2List = backendListBiz.byIdFindCategoryName(queryCategoryLevel1);
        List<AppCategory> categoryLevel3List = backendListBiz.byIdFindCategoryName(queryCategoryLevel2);


        model.addAttribute("queryCategoryLevel1", queryCategoryLevel1);
        model.addAttribute("queryCategoryLevel2", queryCategoryLevel2);
        model.addAttribute("queryCategoryLevel3", queryCategoryLevel3);

        model.addAttribute("categoryLevel2List", categoryLevel2List);
        model.addAttribute("categoryLevel3List", categoryLevel3List);


        System.out.println("值:" + queryCategoryLevel2 + "," + queryCategoryLevel3);

        System.out.println("当前页码" + page.getCurrentPageNo() + "总条数" + page.getPageSize());


        return "forward:jsp/backend/applist.jsp";
    }


    /**
     *  前台基础信息审核页面控制器
     * @param model
     * @param aid
     * @param vid
     * @return
     */
    @RequestMapping("/checkUpdateList")
    public String checkUpdateList(Model model, Integer aid, Integer vid) {

        System.out.println("aid值" + aid + "vid" + vid);
        AppInfo_result appInfo_result=backendListBiz.findAppInfoById(aid);
        AppVersion appVersion=backendListBiz.findAppVersionById(vid);


        model.addAttribute("appInfo", appInfo_result);
        model.addAttribute("appVersion", appVersion);


        return "jsp/backend/appcheck";
    }




    /**
     * 二级联动json
     *
     * @param pid
     * @return
     */
    @RequestMapping("/categoryLevel2")
    @ResponseBody
    public List<AppCategory> categoryLevel2(Integer pid) {


        System.out.println(pid);

        List<AppCategory> categoryLevel2List = backendListBiz.byIdFindCategoryName(pid);


        return categoryLevel2List;
    }

    /**
     * 三级联动
     *
     * @param pid
     * @return
     */
    @RequestMapping("/categoryLevel3")
    @ResponseBody
    public List<AppCategory> categoryLevel3(Integer pid) {


        System.out.println(pid);

        List<AppCategory> categoryLevel3List = backendListBiz.byIdFindCategoryName(pid);


        return categoryLevel3List;
    }


}
